package de.tum.in.eist.im.server.Login;

import de.tum.in.eist.im.client.Login.LoginService;
import de.tum.in.eist.im.shared.Security.SHAConverter;
import de.tum.in.eist.im.shared.User.User;
import de.tum.in.eist.im.server.Datastore.DatastoreManager;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

/**
 * The server side implementation of the RPC service.
 */
@SuppressWarnings("serial")
public class LoginServiceImpl extends RemoteServiceServlet implements
		LoginService {

	/**
	 * TODO: Encrypt password before it is transmited to the server
	 * try to log the user in
	 * @param user the user to login
	 * @return true if login success else false
	 */
	@Override
	public Boolean loginToServer(User user) {
		// encrypt password
		try {
			user.setPassword(SHAConverter.toSHA256(user.getPassword()));
		} catch (Exception e) {
			System.out.println("Couldn't encrypt password on server!");
		}
		// check for valid login
		if (new DatastoreManager().userLoginValidation(user))
			return true;
		else
			return false;
	}
}
